package com.wuyan.locationdog

import android.Manifest
import android.content.Intent
import androidx.fragment.app.Fragment
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.TabBean
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_show_mask
import com.arcsoft.commonbase.bus.bus_fragment_device_detail
import com.arcsoft.commonbase.bus.bus_logout
import com.arcsoft.commonbase.bus.bus_toast
import com.arcsoft.commonbase.constant.Constants.permissions
import com.arcsoft.commonbase.ext.doubleClickExit
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.onTabSelect
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.utils.VpAdapter
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.arcsoft.commonbase.widgets.dialog.ToastBean
import com.arcsoft.commonbase.widgets.dialog.ToastPopup
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.umeng.socialize.UMShareAPI
import com.wuyan.locationdog.databinding.ActivityMainBinding
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.ui.compass.CompassFragment
import com.wuyan.locationdog.ui.map.DeviceDetailFragment
import com.wuyan.locationdog.ui.my.MyFragment
import me.jessyan.autosize.internal.CancelAdapt


class MainActivity : BaseBindingActivity<ActivityMainBinding>() , CancelAdapt {


    private val tabTitles = arrayOf("监控", "罗盘", "我的")
    private val iconUnable = intArrayOf(R.mipmap.tab1, R.mipmap.tab2, R.mipmap.tab3)
    private val iconEnable =
        intArrayOf(R.mipmap.tab1_enable, R.mipmap.tab2_enable, R.mipmap.tab3_enable)


    private val fragments = mutableListOf<Fragment>()
    private val tabs = ArrayList<CustomTabEntity>()



    override fun initData() {
        BarUtils.transparentStatusBar(this)
        PermissionUtils.permission(*permissions)
            .callback { isAllGranted, granted, deniedForever, denied ->
                if (isAllGranted) {

                } else {
                    CommonDialog(mActivity,"打开权限","请打开定位/相机/文件存储权限"){
                        AppUtils.launchAppDetailsSettings()
                    }.show()
                }
            }
            .request()

        showUi()
    }

    override fun onResume() {
        super.onResume()


    }

    private fun showUi() {
        binding.apply {

            tabTitles.forEachIndexed { i, _ ->
                if (LoginHelp.isDeviceLogin) {
                    if (i != 0) {
                        tabs.add(TabBean(tabTitles[i], iconEnable[i], iconUnable[i]))
                    }
                } else {
                    tabs.add(TabBean(tabTitles[i], iconEnable[i], iconUnable[i]))
                }
            }
//            if (LoginHelp.isDeviceLogin) {
//                fragments.add(DeviceHomeFragment())
//                fragments.add(MsgDetailFragment())
//
//            } else {
            fragments.add(DeviceDetailFragment())
            fragments.add(CompassFragment())
//            }

            fragments.add(MyFragment())



            tlBottom.apply {
                setTabData(tabs)
                onTabSelect {
                    vp.currentItem = it
                    fragments.forEachIndexed { index, fragment ->
                        getIconView(index).scaleX = 1.0F
                        getIconView(index).scaleY = 1.0F
                    }
                    getIconView(it).apply {
                        scaleX = 1.7F
                        scaleY = 1.7F
                        pivotY = height.toFloat()
                    }

                    when (it) {
                        0 -> {
                            tlBottom.setBackgroundResource(R.mipmap.tab_1_bg)
                        }

                        1 -> {
                            tlBottom.setBackgroundResource(R.mipmap.tab_2_bg)
                        }

                        else -> {
                            tlBottom.setBackgroundResource(R.mipmap.tab_3_bg)
                        }
                    }
                }
                post {
                    getIconView(0).apply {
                        scaleX = 1.7F
                        scaleY = 1.7F
                        pivotY = height.toFloat()
                    }
                }

            }
            vp.apply {
                offscreenPageLimit = fragments.size
                adapter = VpAdapter(supportFragmentManager, fragments)
            }
        }
    }

    override fun observe() {
        Bus.observe<String>(bus_logout, this) {
            LoginHelp.logout()
        }
        Bus.observe<Boolean>(bus_device_show_mask, this) {
            if (it) {
                binding.tlBottom.gone()
            } else {
                binding.tlBottom.visible()
            }
        }
        Bus.observe<String>(bus_fragment_device_detail, this) {
            binding.apply {
                tlBottom.currentTab = 0
                vp.currentItem = 0
            }
        }

        Bus.observe<ToastBean>(bus_toast, this) {
            ToastPopup(this, it.mTitle).showPopupWindow();

        }
    }

    /**
     * 双击返回
     */
    override fun onBackPressed() {
        if (doubleClickExit()) {
            ActivityUtils.finishAllActivities()
        }


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }
}